package com.ilove.music.frame;

import java.io.IOException;

import javax.microedition.lcdui.Image;

import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConstants;
import org.kalmeo.kuix.core.model.DataProvider;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.ListItem;
import org.kalmeo.kuix.widget.Picture;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.Text;
import org.kalmeo.kuix.widget.Widget;
import org.kalmeo.kuix.widget.Screen.ScreenMenu;
import org.kalmeo.util.frame.Frame;
import com.ilove.media.model.*;
import com.ilove.core.*;

public class ContentListFrame implements Frame {

	public static ContentListFrame instance = new ContentListFrame();
	public int id;// 0 text data from vas, 1 ringtone, 2 game ,3 wallpaper
	public String code;
	public String name;
	private int currPage = 1;
	private int pageSize = 8;
	public String[] vecContent = null;
	String[] contentlist = null;
	boolean isShort = false;
	boolean isnext = true;
	int totalpage = 1;
	private Screen screen;

	// The Pad that hold all shopLists
	private final MediaDataProvider pad = new MediaDataProvider();

	/**
	 * Show the screen
	 */
	public void showScreen() {
		if (screen != null) {
			screen.setCurrent();
		}
	}

	public void onAdded() {
		try {
			int total = pad.getTotalContent(code);
			totalpage = (int) Math.ceil((double)total/(double)pageSize);
			contentlist = pad.getContentList(code,currPage,pageSize);
			
			if (contentlist != null && contentlist.length > 0) {
				// Open the Pad
				pad.setContentList(contentlist);
				screen = Kuix.loadScreen(ILOVEContants.CONTENTLIST_MEDIA_SCREEN, pad);
				screen.setTitle(name);
				isShort = false;
				screen.setCurrent();
			} else {
				ContentFrame.instance.id = id;
				ContentFrame.instance.contentId = code;
				ContentFrame.instance.contentName = name;
				// ContentFrame.instance.pop = pop;
				Kuix.getFrameHandler().pushFrame(ContentFrame.instance);
			}
			Text textPage = (Text)screen.getWidget("textPage");
			textPage.setText(currPage + "/" + totalpage);
			int screenWidth = Kuix.getCanvas().getWidth();
			ScreenMenu rightMenu = (ScreenMenu)screen.getSecondMenu();
			Metrics metrics = rightMenu.getPreferredSize(screenWidth);
			int containerWidth = screenWidth - (2*metrics.width)-10;
			Widget container = screen.getWidget("CTM_PAGE");
			container.setAttribute("style", "layout:flowlayout(true, fill);align:center;min-size:"+containerWidth + " 16");
		} catch (Exception e) {
			Kuix.alert(e);
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onMessage(java.lang.Object,
	 *      java.lang.Object[])
	 */

	public boolean onMessage(Object identifier, Object[] arguments)  {
		if ("askQuit".equals(identifier)) {
			// show askQuit box wich call onMessage method with "quit" name and
			// null arguments
			Kuix.alert(Kuix.getMessage("ASK_QUIT"),
					KuixConstants.ALERT_QUESTION | KuixConstants.ALERT_YES
							| KuixConstants.ALERT_NO, "!exit", null);
			return false;
		} else if ("next".equals(identifier)) {
			Widget focusedWidget = screen.getFocusManager().getFocusedWidget();
			if (focusedWidget instanceof ListItem) {
				DataProvider dataprovider = ((ListItem) focusedWidget)
						.getDataProvider();
				if (dataprovider != null) {
					ContentList list = (ContentList) dataprovider;
					
						try {
							ContentFrame.instance.id = id;
							ContentFrame.instance.contentId = list.code;
							ContentFrame.instance.contentName = list.name;
							// ContentFrame.instance.pop = pop;
							Kuix.getFrameHandler().pushFrame(
									ContentFrame.instance);
						} catch (Exception e) {
						}
					
				}
			}
		} else if ("back".equals(identifier)) {
			Kuix.getFrameHandler().removeFrame(instance);
			return false;
		} else if ("arrLeft".equals(identifier) && isShort) {
			if (currPage > 1)
				currPage--;
			try {
				changePageValue(currPage);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} if ("arrLeft".equals(identifier)) {
			if (currPage > 1) currPage--;
			try {
				changePageValue(currPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if ("arrRight".equals(identifier)) {
			if(isnext)
			{
				currPage++;
				try {
					changePageValue(currPage);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}

	public void changePageValue(int page) throws Exception {
		Text textPage = (Text)screen.getWidget("textPage");
		textPage.setText(currPage + "/" + totalpage);
		Picture pictureLeft = (Picture) screen.getWidget("pic-left");
		Picture pictureRight = (Picture) screen.getWidget("pic-right");
		try {
			if (page == 1) {
				pictureLeft.setImage(Image.createImage("/img/left-dis.png"));
			} else {
				pictureLeft.setImage(Image.createImage("/img/left.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			// get data form 
			contentlist = pad.getContentList(code,currPage,pageSize);
			
			if(contentlist.length !=0)
			{
				pad.setContentList(contentlist);
			}
			// display
			
			if (page == totalpage) {
				isnext = false;
				pictureRight.setImage(Image.createImage("/img/right-dis.png"));
			} else {
				isnext = true;
				pictureRight.setImage(Image.createImage("/img/right.png"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.kalmeo.util.frame.Frame#onAdded()
	 */

	public void onRemoved() {
		// show MainFrame screen
		try {
			currPage =1;
			CategoryFrame.instance.showScreen();
			screen.cleanUp();
			screen = null;
		} catch (Exception e) {
		}

	}
}
